<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c1Id"
          @change="handler"
        >
          <!-- option label显示为文字,value属性即为select下拉菜单收集的数据 -->
          <el-option
            v-for="(c1, index) in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c2Id"
          @change="handler1"
        >
          <el-option
            v-for="(c2, index) in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c3Id"
          @change="handler2"
        >
          <el-option
            v-for="(c3, index) in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
// 引入生命周期函数钩子
import { onMounted } from "vue";
// 引入分类相关的仓库
import useCategoryStore from "@/store/modules/category";

let categoryStore = useCategoryStore();

// 接受父组件传递过来的scene
defineProps(["scene"]);

// 分类全局组件挂载完毕
onMounted(() => {
  // 获取一级分类的数据
  getC1();
});

// 获取一级分类的数据方法
const getC1 = () => {
  categoryStore.getC1();
};

// 当选择了一级分类,选中值的时候触发
const handler = () => {
  categoryStore.c2Id = "";
  categoryStore.c3Arr = [];
  categoryStore.c3Id = "";
  categoryStore.getC2();
};
// 此方法即为二级分类下拉菜单的Change事件
const handler1 = () => {
  categoryStore.c3Id = "";
  categoryStore.getC3();
};
</script>

<style scoped></style>
